Faster Special:Listusers, should be up to 10x faster for long lists
[lhc/web/wiklou.git] / includes / SpecialListusers.php
1 <?php
2 # Copyright (C) 2004 Brion Vibber, lcrocker, Tim Starling,
3 # Domas Mituzas, Ashar Voultoiz, Jens Frank, Zhengzhu.
4 #
5 # http://www.mediawiki.org/
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along
18 # with this program; if not, write to the Free Software Foundation, Inc.,
19 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 # http://www.gnu.org/copyleft/gpl.html
21 /**
22 *
23 * @package MediaWiki
24 * @subpackage SpecialPage
25 */
26
27 /**
28 *
29 */
30 require_once('QueryPage.php');
31
32 /**
33 * This class is used to get a list of user. The ones with specials
34 * rights (sysop, bureaucrat, developer) will have them displayed
35 * next to their names.
36 *
37 * @package MediaWiki
38 * @subpackage SpecialPage
39 */
40 class ListUsersPage extends QueryPage {
41 var $requestedGroup = '';
42 var $requestedUser = '';
43
44 function getName() {
45 return 'Listusers';
46 }
47 function isSyndicated() { return false; }
48
49 /**
50 * Not expensive, this class won't work properly with the caching system anyway
51 */
52 function isExpensive() {
53 return false;
54 }
55
56 /**
57 * Fetch user page links and cache their existence
58 */
59 function preprocessResults( &$db, &$res ) {
60 global $wgLinkCache;
61
62 $batch = new LinkBatch;
63 while ( $row = $db->fetchObject( $res ) ) {
64 $batch->addObj( Title::makeTitleSafe( NS_USER, $row->title ) );
65 }
66 $batch->execute( $wgLinkCache );
67
68 // Back to start for display
69 $db->dataSeek( $res, 0 );
70 }
71
72 /**
73 * Show a drop down list to select a group as well as a user name
74 * search box.
75 * @todo localize
76 */
77 function getPageHeader( ) {
78 global $wgScript;
79
80 // Various variables used for the form
81 $action = htmlspecialchars( $wgScript );
82 $title = Title::makeTitle( NS_SPECIAL, 'Listusers' );
83 $special = htmlspecialchars( $title->getPrefixedDBkey() );
84
85 // form header
86 $out = '<form method="get" action="'.$action.'">' .
87 '<input type="hidden" name="title" value="'.$special.'" />' .
88 wfMsgHtml( 'groups-editgroup-name' ) . '<select name="group">';
89
90 // get all group names and IDs
91 $groups = User::getAllGroups();
92
93 // we want a default empty group
94 $out.= '<option value=""></option>';
95
96 // build the dropdown list menu using datas from the database
97 foreach ( $groups as $group ) {
98 $selected = ($group == $this->requestedGroup);
99 $out .= wfElement( 'option',
100 array_merge(
101 array( 'value' => $group ),
102 $selected ? array( 'selected' => 'selected' ) : array() ),
103 User::getGroupName( $group ) );
104 }
105 $out .= '</select> ';
106
107 $out .= wfMsgHtml( 'specialloguserlabel' ) . '<input type="text" name="username" /> ';
108
109 // OK button, end of form.
110 $out .= '<input type="submit" /></form>';
111 // congratulations the form is now build
112 return $out;
113 }
114
115 function getSQL() {
116 $dbr =& wfGetDB( DB_SLAVE );
117 $user = $dbr->tableName( 'user' );
118 $user_groups = $dbr->tableName( 'user_groups' );
119
120 // We need to get an 'atomic' list of users, so that we
121 // don't break the list half-way through a user's group set
122 // and so that lists by group will show all group memberships.
123 //
124 // On MySQL 4.1 we could use GROUP_CONCAT to grab group
125 // assignments together with users pretty easily. On other
126 // versions, it's not so easy to do it consistently.
127 // For now we'll just grab the number of memberships, so
128 // we can then do targetted checks on those who are in
129 // non-default groups as we go down the list.
130
131 $userspace = NS_USER;
132 $sql = "SELECT 'Listusers' as type, $userspace AS namespace, user_name AS title, " .
133 "user_name as value, user_id, COUNT(ug_group) as numgroups " .
134 "FROM $user ".
135 "LEFT JOIN $user_groups ON user_id=ug_user " .
136 $this->userQueryWhere( $dbr ) .
137 " GROUP BY user_name";
138
139 return $sql;
140 }
141
142 function userQueryWhere( &$dbr ) {
143 $conds = $this->userQueryConditions();
144 return empty( $conds )
145 ? ""
146 : "WHERE " . $dbr->makeList( $conds, LIST_AND );
147 }
148
149 function userQueryConditions() {
150 $conds = array();
151 if( $this->requestedGroup != '' ) {
152 $conds['ug_group'] = $this->requestedGroup;
153 }
154 if( $this->requestedUser != '' ) {
155 $conds['user_name'] = $this->requestedUser;
156 }
157 return $conds;
158 }
159
160 function linkParameters() {
161 $conds = array();
162 if( $this->requestedGroup != '' ) {
163 $conds['group'] = $this->requestedGroup;
164 }
165 if( $this->requestedUser != '' ) {
166 $conds['username'] = $this->requestedUser;
167 }
168 return $conds;
169 }
170
171 function sortDescending() {
172 return false;
173 }
174
175 function formatResult( $skin, $result ) {
176 global $wgContLang;
177
178 $userPage = Title::makeTitle( $result->namespace, $result->title );
179 $name = $skin->makeLinkObj( $userPage, htmlspecialchars( $userPage->getText() ) );
180
181 if( !isset( $result->numgroups ) || $result->numgroups > 0 ) {
182 $dbr =& wfGetDB( DB_SLAVE );
183 $result = $dbr->select( 'user_groups',
184 array( 'ug_group' ),
185 array( 'ug_user' => $result->user_id ),
186 'ListUsersPage::formatResult' );
187 $groups = array();
188 while( $row = $dbr->fetchObject( $result ) ) {
189 $groups[] = User::getGroupName( $row->ug_group );
190 }
191 $dbr->freeResult( $result );
192
193 if( count( $groups ) > 0 ) {
194 $name .= ' (' .
195 $skin->makeLink( wfMsgForContent( 'administrators' ),
196 htmlspecialchars( implode( ', ', $groups ) ) ) .
197 ')';
198 }
199 }
200
201 return $name;
202 }
203 }
204
205 /**
206 * constructor
207 * $par string (optional) A group to list users from
208 */
209 function wfSpecialListusers( $par = null ) {
210 global $wgRequest;
211
212 list( $limit, $offset ) = wfCheckLimits();
213
214
215 $slu = new ListUsersPage();
216
217 /**
218 * Get some parameters
219 */
220 $groupTarget = isset($par) ? $par : $wgRequest->getVal( 'group' );
221 $slu->requestedGroup = $groupTarget;
222 $slu->requestedUser = $wgRequest->getVal('username');
223
224 return $slu->doQuery( $offset, $limit );
225 }
226
227 ?>